package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.servercenter.ChannelSystemConfig;
import com.yanqu.road.entity.servercenter.ConfigVersionCheckInfo;
import com.yanqu.road.entity.servercenter.SystemLoopConfig;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;

public class ConfigDaoImpl extends TempDao {

	public HashMap<String, String> getGameConfig() {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_p_param";
		HashMap<String, String> dataMap = new HashMap<>();
		try {
			getDb().setConnName(DbNameStrategy.getConfigDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				String name = rs.getString("param");
				if (!dataMap.containsKey(name)) {
					dataMap.put(name, rs.getString("value"));
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	public HashMap<String, String> getChannelConfig(int channelId) {
		ResultSet rs = null;
		CallableStatement call = null;
		HashMap<String, String> dataMap = new HashMap<>();
		String sql = "select * from t_s_param where channelId = ?";
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setInt(1, channelId);
			rs = call.executeQuery();
			while (rs.next()) {
				String name = rs.getString("param");
				if (!dataMap.containsKey(name)) {
					dataMap.put(name, rs.getString("value"));
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	public HashMap<String, String> getServerConfig(long serverId) {
		ResultSet rs = null;
		CallableStatement call = null;
		HashMap<String, String> dataMap = new HashMap<>();
		String sql = "select * from t_s_server_param where serverId = ?";
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, serverId);
			rs = call.executeQuery();
			while (rs.next()) {
				String name = rs.getString("param");
				if (!dataMap.containsKey(name)) {
					dataMap.put(name, rs.getString("value"));
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	public Map<Integer, Map<String, ChannelSystemConfig>> getChannelSystemConfigMap(int channelId) {
		ResultSet rs = null;
		CallableStatement call = null;
		Map<Integer, Map<String, ChannelSystemConfig>> dataMap = new HashMap<>();
		String sql = "select * from t_s_system_config where channelId = ?";
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setInt(1, channelId);
			rs = call.executeQuery();
			while (rs.next()) {
				int systemId = rs.getInt("systemId");
				String name = rs.getString("param");
				String value = rs.getString("value");
				long modifyTime = rs.getLong("modifyTime");
				if (!dataMap.containsKey(systemId)) {
					dataMap.put(systemId, new HashMap<>());
				}
				dataMap.get(systemId).put(name, new ChannelSystemConfig(systemId, name, value, channelId, modifyTime));
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	public Map<Integer, SystemLoopConfig> getSystemLoopConfigMap(int channelId) {
		ResultSet rs = null;
		CallableStatement call = null;
		Map<Integer, SystemLoopConfig> dataMap = new HashMap<>();
		String sql = "select * from t_s_system_loop_config where channelId = ?";
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setInt(1, channelId);
			rs = call.executeQuery();
			while (rs.next()) {
				int id = rs.getInt("id");
				String openDays = rs.getString("openDays");
				String systemIds = rs.getString("systemIds");
				long modifyTime = rs.getLong("modifyTime");
				SystemLoopConfig config = new SystemLoopConfig();
				config.setId(id);
				config.setChannelId(channelId);
				config.setOpenDays(openDays);
				config.setSystemIds(systemIds);
				config.setModifyTime(modifyTime);

				dataMap.put(id, config);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	public ConfigVersionCheckInfo getConfigVersionCheckInfo(int channelId) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_s_config_version_check where channelId = ?";
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setInt(1, channelId);
			rs = call.executeQuery();
			while (rs.next()) {
				ConfigVersionCheckInfo info = new ConfigVersionCheckInfo();
				info.setChannelId(channelId);
				info.setConfigVersion(rs.getString("configVersion"));
				info.setMgrNames(rs.getString("mgrNames"));
				return info;
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return null;
	}
}
